<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/14
 * Time: 14:09
 */

namespace app\common\service;


use app\adminapiv3\service\attackLogManage\AttackLogService;
use function Couchbase\fastlzCompress;

class NetworkAttack
{
    /**
     * @name 列表
     * @param $params
     * @param $limit
     * @return array
     * @author wx
     * @date 2019/11/14 14:42
     */
    function list($params, $limit)
    {
        $field = 'id, host_id, description, src_ip, dest_ip, domain_name, url,
          atime, type, level, handle, rule_id, event_id, allow_ip_rule_id, ctime';

        $db = db('network_attack');

        isset($params['host_id']) && !empty($params['host_id']) ? $db->whereIn('host_id', $params['host_id']): '';
        !isset($params['description']) || empty($params['description']) ? : $db->whereLike('description', '%'. $params['description'] . '%');
        !isset($params['domain_name']) || empty($params['domain_name']) ? : $db->whereLike('domain_name', '%'. $params['domain_name'] .'%');
        !isset($params['src_ip']) || empty($params['src_ip']) ? : $db->whereLike('src_ip', '%'. $params['src_ip'] .'%');
        !isset($params['dest_ip']) || empty($params['dest_ip']) ? : $db->whereLike('dest_ip', '%'. $params['dest_ip'] .'%');
        !isset($params['url']) || empty($params['url']) ? : $db->whereLike('url', '%'. $params['url'] .'%');
        if (isset($params['atime']) && !empty($params['atime']))
        {
            $stime = strtotime($params['atime']);
            $etime = $stime + 86400;
            $db->whereBetween('atime', [$stime,$etime]);
        }
        !isset($params['type']) || !is_numeric($params['type']) ? : $db->where('type', $params['type']);
        !isset($params['level']) || !is_numeric($params['level']) ? : $db->where('level', $params['level']);

        //处理结果
        if (isset($params['handle']) && is_numeric($params['handle'])) {
            if(in_array($params['handle'], [1, 32])){
                $db->where('handle', $params['handle']);
            }else{
                $db->whereNotIn('handle', [1,32]);
            }
        }


        $return = $db->field($field)->order('atime desc')->paginate($limit)->toArray();
        //$return = $db->field($field)->order('atime desc')->limit($limit)->select(false);


        return $return;
    }

    /**
     * @name 查询数据
     * @param $condition
     * @param string $field
     * @return array|null|\PDOStatement|string|\think\Model
     * @author wx
     * @date 2019/11/15 14:40
     */
    function findData($condition, $field = '*')
    {
        $info = db('network_attack')
            ->field($field)
            ->where($condition)->find();

        return $info;
    }

    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/11/15 14:41
     */
    function modify($condition, $updateData)
    {
        return db('network_attack')->where($condition)->update($updateData);
    }

    /**
     * @name: 统计
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-09-05 14:56:53
     */
    function ratio ()
    {
        $return = db('NetworkAttack')->field('type, count(*) as value')->group('type')->select();

        return $return;
    }

    /**
     * @name 告警提示
     * @param $time
     * @return mixed
     * @author wx
     * @date 2020/6/9 13:09
     */
    function warning(){

        $server = new AttackLogService();
        $host_id_s = $server->belongedHost();

        $attackCount = db('network_attack')
            ->whereIn('host_id', $host_id_s)
            ->where('is_look', 0)
            ->count();


        $list['count'] = $attackCount;

        $msg = '';
        if($attackCount != 0){

            $fields = 'na.ctime, na.level, na.type, na.src_ip, na.dest_ip, it.show_name';
            $info = db('network_attack')->alias('na')
                ->join('ips_type it', 'na.type = it.id')
                ->field($fields)
                ->where('is_look',0)
                ->order('na.ctime', 'desc')
                ->limit(1)
                ->find();

            $msg = date('Y-m-d H:i:s'). ' 攻击告警 目标 ['.$info['dest_ip'].'] 受到来自 ['.$info['src_ip'].'] 的 ['.$info['show_name'].'] 攻击及其他攻击共计 ['.$attackCount.'] 次';

            $this->sendEmail($msg);
        }


        $list['tip_msg'] = $msg;

        return $list;
    }

    /**
     * @name 发送邮箱 告警
     * @author wx
     * @date 2020/7/24 15:08
     */
    function sendEmail($content)
    {
        //异步发送电子邮件
        $url = $_SERVER['REQUEST_SCHEME'].'://' . $_SERVER['HTTP_HOST'] .'/adminapi/v3/email/manage/public/send';
        $sendData['title'] = '预警信息';
        $sendData['send_content'] = $content;

        curlPost($url, $sendData);

//        $emailServer = new \app\adminapiv3\service\Email();
//        $emailServer->sendEmail('预警信息', $content);
    }
}